package com.cw.lang.common.exception;


import com.cw.lang.common.constants.HttpStatusConstant;

import lombok.Getter;

/**
 * 系统异常
 *
 * @author chenwei
 * @version $$Id: SystemException.java, v 0.1 2019/2/25 9:59 AM chenwei Exp $$
 */
@Getter
public class SystemException extends AbstractExceptionWrapper {
    private static final long serialVersionUID = 5205892938661742662L;

    /** http请求结果code */
    private Integer httpStatus;

    /**
     * 异常构造函数
     *
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param params 描述中的占位符替换值
     */
    public SystemException(Integer httpStatus, String errorCode, String errorMessage, String... params) {
        super(format(errorMessage, (Object[]) params));
        this.httpStatus = httpStatus == null ? HttpStatusConstant.OK : httpStatus;
        this.errorCode = errorCode;
        this.errorMessage = format(errorMessage, (Object[]) params);
    }

    /**
     * 异常构造函数
     *
     * @param t 异常
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param params 描述中的占位符替换值
     */
    public SystemException(Throwable t, Integer httpStatus, String errorCode, String errorMessage,
        String... params) {
        super(format(errorMessage, (Object[]) params), t);
        this.httpStatus = httpStatus == null ? HttpStatusConstant.OK : httpStatus;
        this.errorCode = errorCode;
        this.errorMessage = format(errorMessage, (Object[]) params);
    }

    /**
     * 异常构造函数，为了兼容老的异常
     *
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param t 异常
     */
    public SystemException(Integer httpStatus, String errorCode, String errorMessage, Throwable t) {
        super(errorMessage);
        this.httpStatus = httpStatus == null ? HttpStatusConstant.OK : httpStatus;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    /**
     * 异常构造函数，为了兼容老的异常
     *
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param clazz 抛出异常的class类
     * @param t 异常
     */
    public SystemException(Integer httpStatus, String errorCode, String errorMessage, Class<?> clazz,
        Throwable t) {
        super(errorMessage, t);
        this.httpStatus = httpStatus == null ? HttpStatusConstant.OK : httpStatus;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.clazz = clazz;
    }

    /**
     * 异常构造函数
     *
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param clazz 抛出异常的class类
     * @param params 描述中的占位符替换值
     */
    public SystemException(Integer httpStatus, String errorCode, String errorMessage, Class<?> clazz,
        String... params) {
        super(format(errorMessage, (Object[]) params));
        this.httpStatus = httpStatus == null ? HttpStatusConstant.OK : httpStatus;
        this.errorCode = errorCode;
        this.errorMessage = format(errorMessage, (Object[]) params);
        this.clazz = clazz;
    }

    /**
     * 异常构造函数
     *
     * @param t 异常
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param clazz 抛出异常的class类
     * @param params 描述中的占位符替换值
     */
    public SystemException(Integer httpStatus, Throwable t, String errorCode, String errorMessage, Class<?> clazz,
        String... params) {
        super(format(errorMessage, (Object[]) params), t);
        this.httpStatus = httpStatus == null ? HttpStatusConstant.OK : httpStatus;
        this.errorCode = errorCode;
        this.errorMessage = format(errorMessage, (Object[]) params);
        this.clazz = clazz;
    }
}
